<?php
// +----------------------------------------------------------------------
// | Created by PhpStorm
// +----------------------------------------------------------------------
// | Date: 2020/6/29 11:24
// +----------------------------------------------------------------------
// | Use : 说明用途，主要方面！
// +----------------------------------------------------------------------
// | Author: Freedom <961449954@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller\system;
use app\common\controller\AdminController;
use think\App;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

class Organ extends AdminController{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\mall\Store');
    }
    public function edit()
    {
        $admin = Session::get('admin');

        $row = $this->model->find($admin['store_id']);

        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            try {
                $post = $this->request->post();
               
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }
                $this->validate($post, $rule);

                $post['image'] = implode(',',$post['image']);
                $post['service_phone'] = implode(',',$post['service_phone']);
                Db::name('store')->strict(false)->where('id',$row['id'])->update($post);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
           $this->success('保存成功');
        }
        View::assign('row', $row);
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view('', compact('row'));
    }

    /**
     * 获取验证规则
     * @param $post
     * @return array
     */
    public function get_check_rule($post)
    {
        $rule = [
            'description|机构简介'   => 'require|max:15',
            'image|机构轮播图'   => 'require',
            'service_phone|服务电话'   => 'require',
            'email|机构邮箱'   => 'email',


        ];

        return $rule;
    }
}